/*
 * gy_enpctrl.h
 *
 *  Created on: 2015年9月15日
 *      Author: gaoyi
 */

#ifndef GY_ENPCTRL_H_
#define GY_ENPCTRL_H_

#include "gy_enpdef.h"
#include "net/CTEpoll.h"
#include "gy_enplisten.h"
#include "gy_enpclient.h"
#include "gy_enpclientfactory.h"

using namespace CTLib;


//enp class
class GYEnpCtrl
{
public:

public:
	friend class GYEnpListen;
	friend class GYEnpClient;
public:
	int		Init();
	int		Run();
	int		Exit();
public:
    void OnShmQueueEvent(CTEpollObject<GYEnpCtrl>* pstObject, SOCKET iSocket, int iEvent);
    void OnListenEvent(CTEpollObject<GYEnpCtrl>* pstObject, SOCKET iSocket, int iEvent);
    void OnClientEvent(CTEpollObject<GYEnpCtrl>* pstObject, SOCKET iSocket, int iEvent);
public:
	void DumpNetHead(CEnpNetHead& stHead);
	void DumpHandleInfo(GYEnpClient* pstClient);
	void DumpStatInfo();
public:

    void HandleShmQueueMsg();

	/**
	* 判断RecvBuf中是否有完整的消息.
    * @param[out] iLen 是整个消息的长度
    * @param[out] pszMsgLen 是消息中有效数据的起始位置
    * @param[out] iMsgLen 是消息中有效数据的长度
	*/
	int BufferHasMessage(GYEnpClient* pstClient, int& iLen, char*& pszMsgBuf, int& iMsgLen);

	// 把RecvBuf中的消息放入接收队列
    int PutOneMessage(GYEnpClient* pstClient, int iLen, const char* szMsgBuf, int iMsgLen);

	// 向上层发送Notify
	int PutOneNotify(GYEnpClient* pstClient, unsigned short unLiveFlag);
    int PutOneNotify(CEnpNetHead& stNetHead, unsigned short unLiveFlag);

	int RecvSocket(GYEnpClient* pstClient);
	int SendSocket(GYEnpClient* pstClient);
	int	CloseSocket(GYEnpClient* pstClient, unsigned short unLiveFlag);

    int AfterRecvSocket(GYEnpClient* pstClient);
    int AfterSendSocket(GYEnpClient* pstClient);
private:
	CTEpoll						m_stEpoll;
	CTShmQueue<GYEnpCtrl>		m_stShmQueue;
	GYEnpListen					m_stListen;
	GYEnpClientFactory			m_stClientFactory;
	CTBuffer			   		m_stTempBuf;		// 临时缓冲区
private:
	unsigned int				m_uiNow;
};



#endif /* GY_ENPCTRL_H_ */
